var $ = getApp(),
  methods = require("../methods.js");
Page(Object.assign({}, methods, {
  data: {
    isfree: 0,
    selectedDate: '',
    selectedDateEnd: '',
    selectedDateChouhao: '',
    datalist: [],
    pagenum: 1,
    cateArr: [],
    diaodianlist: [],
    diaodianArr: [],
    diaochangArr: [],
    pics: [],
    cindex: 0,
    dindex: -1,
    isagree: true,
    id: 0,
    diaodian_id: 0,
    selectIds: [],
    showleixing: false,

    type: 1,
    startYear: 2023,
    endYear: 2099,
    cancelColor: "#888",
    color: "#5677fc",
    setDateTime: "",
    result: "",
    array: ['开场统一抽号', '随到随钓', '开场抽号，后续随到随钓'],
    index: 0,
    diaochangidArr: []
  },
  _onLoad: function () {
    var that = this;
    var id = 0;
    if (that.options.id) {
      id = that.options.id;
      that.setData({
        id: id
      })
    }
    $.get('yuxun/fatiediaodian4', {
      id: id
    }, function (res) {
      const names = res.diaochanglist.map(item => item.name)
      const dcids = res.diaochanglist.map(item => item.id)
      console.log('names321', names)
      that.setData({
        diaochangArr: names,
        diaochangidArr: dcids
      })
      if (res.status == 2) {
        $.alert(res.msg, function () {
          if (res.tourl) {
            $.goto(res.tourl, 'redirect');
          }
        });
        return;
      }
      var clist = res.clist
      var cateArr = [];
      for (var i in clist) {
        cateArr.push(clist[i].name)
      }
      var pics = res.info ? res.info.pics : '';
      if (pics) {
        pics = pics.split(',');
      } else {
        pics = [];
      }

      var diaodianlist = res.diaodianlist;
      var diaodianArr = [];
      var dindex = 0;
      for (var i in diaodianlist) {
        diaodianArr.push(diaodianlist[i].name);
        if (res.info.diaochang_id == diaodianlist[i].id) {
          dindex = i;
        }
      }
      that.setData({
        dindex: dindex
      })


      res.cateArr = cateArr;
      res.pics = pics;
      res.diaodianArr = diaodianArr;
      if (res.info.starttime) {
        that.setData({
          selectedDate: res.info.starttime
        })
      }
      if (res.info.endtime) {
        that.setData({
          selectedDateEnd: res.info.endtime
        })
      }
      if (res.info.chouhaotime) {
        that.setData({
          selectedDateChouhao: res.info.chouhaotime
        })
      }
      res.diaodian_id = res.info.diaochang_id;

      console.log(res)
      that.setData(res)
    });
  },
  onReady: function (options) {
    this.dateTime = this.selectComponent("#tui-dateTime-ctx")
  },
  showleixing: function () {
    this.setData({
      showleixing: true
    })
  },
  hideleixing: function () {
    this.setData({
      showleixing: false
    })
  },

  // 抽号方式
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  changeTagType2: function (e) {
    var id = e.currentTarget.dataset.id;
    var selectIds = this.data.selectIds;

    var idx = e.currentTarget.dataset.index;
    let clist = [...this.data.clist];

    let obj = {};
    clist.forEach((item, index) => {
      if (idx === index) {
        obj.id = item.id;
        obj.name = item.name;
        obj.select = !item.select;
        clist.splice(index, 1, obj);
        obj = {};
      }
    });

    var selectIds = [];
    for (var i = 0; i < clist.length; i++) {
      if (clist[i].select == true) {
        selectIds.push(clist[i].id)
      }
    }
    console.log(selectIds)

    this.setData({
      clist: clist,
      selectIds: selectIds
    })
  },
  freeChange: function (e) {
    var isfree = e.detail.value;
    console.log(isfree)
    this.setData({
      isfree: isfree
    })
  },
  cateChange: function (e) {
    this.setData({
      cindex: e.detail.value
    })
  },
  diaodianChange: function (e) {
    console.log('钓场', e)
    var dindex = e.detail.value;
    // var diaodian_id = this.data.diaodianlist[dindex].id;
    this.setData({
      dindex: dindex,
      // diaodian_id:diaodian_id
    });
  },
  handleDateChange: function (event) {
    const selectedDate = event.detail.value;
    console.log(selectedDate)
    this.setData({
      selectedDate: selectedDate,
    });
  },
  handleDateChangeEnd: function (event) {
    const selectedDateEnd = event.detail.value;
    this.setData({
      selectedDateEnd: selectedDateEnd,
    });
  },
  handleDateChangeChouhao: function (event) {
    const selectedDateChouhao = event.detail.value;
    this.setData({
      selectedDateChouhao: selectedDateChouhao,
    });
  },
  locationSelect: function () {
    var that = this
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          'info.address': res.name,
          'info.latitude': res.latitude,
          'info.longitude': res.longitude
        })
      }
    })
  },
  subform: function (e) {
    var that = this;
    var info = e.detail.value;
    var selectIds = this.data.selectIds;
    if (info.name == '') {
      $.error('请输入钓点名称');
      return false;
    }
    if (info.latitude == '') {
      $.error('请选择坐标');
      return false;
    }
    if (info.address == '') {
      $.error('请输入详细地址');
      return false;
    }
    if (selectIds.length == 0) {
      $.error("请选择钓点类型");
      return false;
    }

    info.cid = selectIds.join(",");
    // if(info.shuishen=='' || info.ganchang=="" || info.yonger==""){
    // 	$.error('请输入钓点信息');return false;
    // }
    info.diaochang_id = that.data.diaodian_id;


    if (info.pics == '') {
      $.error('请上传钓点图片');
      return false;
    }

    if (that.data.selectedDate) {
      info.starttime = that.data.selectedDate
    } else {
      info.starttime = "";
      $.error('请选择开钓时间');
      return false;
    }

    if (that.data.selectedDateEnd) {
      info.endtime = that.data.selectedDateEnd
    } else {
      info.endtime = "";
      $.error('请选择结束时间');
      return false;
    }

    if (that.data.selectedDateChouhao) {
      info.chouhaotime = that.data.selectedDateChouhao
    } else {
      info.chouhaotime = "";
      $.error('请选择抽号时间');
      return false;
    }

    //info.cid = that.data.clist[that.data.cindex].id
    // if(that.data.info && that.data.info.id){
    // 	info.id = that.data.info.id;
    // }

    //info.isfree = that.data.isfree;
    var id = that.data.id;
    console.log('放钓信息', info)
    $.post("yuxun/fatiediaodian4", {
      info: info,
      id: id,
      diaochang_id: that.data.diaochangidArr[that.data.dindex],
      chouhaoway: that.data.array[that.data.index]
    }, function (res) {
      $.error(res.msg);
      if (res.status == 1) {
        setTimeout(function () {
          $.goback(true)
        }, 2000)
      }

    }, '提交中');
  },
  isagreeChange: function (e) {
    console.log(e.detail.value)
    var val = e.detail.value
    if (val.length > 0) {
      this.setData({
        isagree: true
      })
    } else {
      this.setData({
        isagree: false
      })
    }
  },
  showxieyi: function () {
    this.setData({
      showxieyi: true
    })
  },
  hidexieyi: function () {
    this.setData({
      showxieyi: false,
      isagree: true
    })
  },
  setname: function (e) {
    this.setData({
      'info.name': e.detail.value
    })
  },
  setprice: function (e) {
    this.setData({
      'info.price': e.detail.value
    })
  },
  setshuishen: function (e) {
    this.setData({
      'info.shuishen': e.detail.value
    })
  },
  setganchang: function (e) {
    this.setData({
      'info.ganchang': e.detail.value
    })
  },
  setyonger: function (e) {
    this.setData({
      'info.yonger': e.detail.value
    })
  },

  setfield(e) {
    var field = e.currentTarget.dataset.name;
    if (field == "diaowei") {
      this.setData({
        'info.diaowei': e.detail.value
      })
    }
    if (field == "quxiaotime") {
      if(e.detail.value > 0){
        this.setData({
          'info.quxiaotime': e.detail.value
        })
      }else{
        wx.showModal({
          title: '提示',
          content: '开钓取消报名时间应该大于1',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              
            }
          }
        })
      }
      
    }
    if (field == "huiyuprice") {
      this.setData({
        'info.huiyuprice': e.detail.value
      })
    }

    if (field == "xiangou") {
      this.setData({
        'info.xiangou': e.detail.value
      })
    }
    if (field == "diyu") {
      this.setData({
        'info.diyu': e.detail.value
      })
    }
    if (field == "fangyu") {
      this.setData({
        'info.fangyu': e.detail.value
      })
    }
    if (field == "huiyu") {
      this.setData({
        'info.huiyu': e.detail.value
      })
    }

  },
  showTime: function (e) {
    let type = Number(e.currentTarget.dataset.type);
    let fieldname = e.currentTarget.dataset.fieldname;
    var setDateTime = "";
    if (fieldname == "selectedDate") {
      setDateTime = this.data.selectedDate
    }
    if (fieldname == "selectedDateEnd") {
      setDateTime = this.data.selectedDateEnd
    }
    if (fieldname == "selectedDateChouhao") {
      setDateTime = this.data.selectedDateChouhao
    }
    this.setData({
      cancelColor: "#888",
      color: "#5677fc",
      setDateTime: setDateTime,
      //startYear: 2023,
      //endYear: 2099,
      fieldname: fieldname
    })
    console.log(fieldname)
    switch (type) {
      case 1:
        this.setData({
          type: 2
        })
        break;
      case 2:
        this.setData({
          type: 3
        })
        break;
      case 3:
        this.setData({
          type: 4
        })
        break;
      case 4:
        this.setData({
          type: 1
        })
        break;
      case 5:
        this.setData({
          type: 1,
          setDateTime: "2019-09-12 18:01"
        })
        break;
      case 6:
        this.setData({
          type: 1,
          startYear: 2019,
          endYear: 2021
        })
        break;
      case 7:
        this.setData({
          type: 1,
          cancelColor: "#555",
          color: "#e41f19"
        })
        break;
      default:
        break;
    }
    this.dateTime.show();
  },
  changeTime: function (e) {
    console.log(e)
    var fieldname = this.data.fieldname;
    if (fieldname == "selectedDate") {
      this.setData({
        selectedDate: e.detail.result
      })
    }
    if (fieldname == "selectedDateEnd") {
      this.setData({
        selectedDateEnd: e.detail.result
      })
    }
    if (fieldname == "selectedDateChouhao") {
      this.setData({
        selectedDateChouhao: e.detail.result
      })
    }
  }
}))